package com.rms.recruit_management_system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rms.recruit_management_system.mapper.PositionVoMapper;
import com.rms.recruit_management_system.pojo.Positions;
import com.rms.recruit_management_system.service.impl.AdministratorServiceImpl;
import com.rms.recruit_management_system.service.impl.PositionsServiceImpl;
import com.rms.recruit_management_system.utils.Result;
import com.rms.recruit_management_system.vo.PositionVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;


@Slf4j
@RestController
@Tag(name = "职位信息控制类", description = "用于对职位信息进行业务操作")
public class PositionsController {
    @Autowired
    private PositionsServiceImpl positionsService;
    @Autowired
    private PositionVoMapper positionVoMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Operation(summary = "获取所有职位信息",description = "获取所有职位信息")
    @RequestMapping("/positions/list")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer current,
                              @RequestParam(defaultValue = "10") Integer size,
                              @RequestParam(defaultValue = "") String search){
        String key = "positions_" + current + "_" + size + "_" + search;
        IPage<Positions> page=(IPage)redisTemplate.opsForValue().get(key);

        if(page != null && page.getTotal()>0){
            log.info("redis", page);
            return new Result<>(200,"SUCCESS",page);
        }
         page = positionsService.getPositionsPage(current, size, search);
        log.info("mysql",page);
        redisTemplate.opsForValue().set(key,page,60,TimeUnit.SECONDS);
        return new Result<>(200, "SUCCESS", page);
    }



    @Operation(summary = "添加职位信息",description = "添加职位信息")
    @RequestMapping("/positions/add")
    public Result<?> addPositions(@RequestBody Positions positions){
        positionsService.addPositions(positions);
        cleanCache("positions_*");
        return new Result<>(200,"SUCCESS",positions);
    }
    @Operation(summary = "修改职位信息",description = "根据id修改职位信息")
    @RequestMapping("/positions/update")
    public Result<?> updatePositions(@RequestBody Positions positions){
        positionsService.updatePositions(positions);
        cleanCache("positions_*");
        return new Result<>(200,"SUCCESS",positions);
    }
    @Operation(summary = "删除职位信息",description = "根据id删除职位信息")
    @RequestMapping("/positions/delete/{id}")
    public Result<?> deletePositions(@PathVariable Integer id){
        positionsService.deletePositions(id);
        cleanCache("positions_*");
        return new Result<>(200,"SUCCESS");
    }

    @Operation(summary = "批量删除职位信息",description = "根据id批量删除职位信息")
    @RequestMapping("/positions/deleteBatch")
    public Result<?> deleteBatchPositions(@RequestBody List<Integer> positionIds){
        try {
            positionsService.batchDeletePositions(positionIds);
            cleanCache("positions_*");
            return new Result<>(200,"SUCCESS");
        }catch (Exception e){
            return new Result<>(400,"FAIL");
        }
    }

    @Operation(summary = "查找职位信息",description = "根据部门名称查找职位信息")
    @RequestMapping("/positions/getPositionsVo/{departmentName}")
    public Result<?> getPositionByDepartmentName(@PathVariable String departmentName){
        String key="positions_" + departmentName;
        List<PositionVo> positionVo=(List<PositionVo>) redisTemplate.opsForValue().get(key);
        if(positionVo!=null && positionVo.size()>0){
            return new Result<>(200,"SUCCESS",positionVo);
        }
         positionVo = positionVoMapper.getPositionVo(departmentName);
        redisTemplate.opsForValue().set(key,positionVo,60,TimeUnit.SECONDS);
        return new Result<>(200,"SUCCESS",positionVo);
    }

    @Operation(summary = "查找职位信息",description = "根据学历查找职位信息")
    @RequestMapping("/positions/getPositionsByDegree/{degree}")
    public Result<?> getPositionsByDegree(@PathVariable String degree){
        String key="positions_" + degree;
        List<Positions> positions = (List<Positions>) redisTemplate.opsForValue().get(key);
        if(positions!=null && positions.size()>0){
            return new Result<>(200,"SUCCESS",positions);
        }
        positions = positionsService.getPositionsByDegree(degree);
        redisTemplate.opsForValue().set(key,positions,60,TimeUnit.SECONDS);
        return new Result<>(200,"SUCCESS",positions);
    }

    @Operation(summary = "查找职位信息",description = "根据id查找职位信息")
    @RequestMapping("/positions/getPositionsById/{positionId}")
    public Result<?> getPositionsById(@PathVariable Integer positionId){
        String key="positions_" + positionId;
        Positions positions = (Positions) redisTemplate.opsForValue().get(key);
        if(positions!=null){
            return new Result<>(200,"SUCCESS",positions);
        }
        positions = positionsService.getPositionsById(positionId);
        redisTemplate.opsForValue().set(key,positions,60,TimeUnit.SECONDS);
        return new Result<>(200,"SUCCESS",positions);
    }

    /**
     * 清空缓存
     * @param pattern
     */
    public void cleanCache(String pattern){
        Set keys=redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
